import Helper from '../../lib'
import _ from 'lodash'
const namespaced = true

const emptyState = {
  countryCode: null,
  countryName: null,
  email: null,
  firstName: null,
  idCard: null,
  mobile: null,
  realName: null,
  uid: null,
  username: null,
  visitCode: null,
  token: null,
}

const state = _.cloneDeep(emptyState, Helper.getCache('userInfo'))

const mutations = {
  SETLOGINDATA(state, data) {
    Object.assign(state, data)
  },
  CLEARLOGINDATA(state, data) {
    Object.assign(state, _.cloneDeep(emptyState))
  },
}

const actions = {
  setLoginData({ commit }, data) {
    let { dontSave } = data
    if (!dontSave) Helper.setCache('userInfo', data)  // save to cache
    commit('SETLOGINDATA', data)
  },
  clearLoginData({ commit }) {
    Helper.setCache('userInfo', emptyState)     // removeCache
    commit('CLEARLOGINDATA')
  },
}

export default { namespaced, state, mutations, actions }
